<?php
class WorkAction extends Action {
	private $uid = '';
	/*======================================================================*\
		Function:	_initialize
		Purpose:	初始化一些参数，主要判断是否登录授权
		Input:		无	            空
		Output:		无      		空
	\*======================================================================*/
	public function _initialize(){
		
		$this->_uid = session('uid');
		if(!$this->_uid) $this->redirect("Public/login");//(503,"对不起，您登录超时了，请重新登录！",1);
		//B('Authenticate', $action);
	}
	/*======================================================================*\
		Function:	worklog
		Purpose:	查看工作日志
		Input:		无	            空
		Output:		模板输出      	#
	\*======================================================================*/
	public function worklog(){
		$Worklog = M('Worklog');
		$Role = M('UserRole');
		$Department = M('Department');
		import('ORG.Util.Page');
		$param = $this->_param();
		$uid = $this->_uid;
		$role = $Role->where("owner_uid = $uid")->find();
		switch($role['role_id']){
			case '1':
			    //管理员
				$w = array(
					'A.status' => 1	
				);
			    break;
			case '2':
				//部门负责人所属部门
				$departmentID = $Department->where("owner_uid = $uid")->getField('department_id');
				//当前部门下的所有用户
				$deptUID = $Department->where("department_id = $departmentID")->field('owner_uid')->select();
			    $w = array(
					'A.owner_uid' => array('in',$deptUID),
					'A.status' => 1
				);
			    break;
			case '0':
				//部门成员
			    $w = array(
					'A.owner_uid' => $uid,
					'A.status' => 1
				);
			    break;
			default:
				$w = array(
					'A.owner_uid' => $uid,
					'A.status' => 1
				);
				break;
		}
		
		$count      = $Worklog->alias('A')->where($w)->count();// 查询满足要求的总记录数
		$results = $Worklog->field('A.*,B.name')->alias('A')->join('oa_user_profile B ON A.owner_uid = B.owner_uid')->where($w)->order('A.update_time DESC')->select();
		
		//echo $Worklog->getLastSql();
		$Page       = new Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		$this->assign('page',$show);
		//dump($results);
		//echo $Worklog->getLastSql();
		//echo count($results);
		if(!$results && $_GET['type'] == 1){   //type == 1 动作：首次查看日志列表
			$this->ajaxReturn(0,'日志列表为空',0);
		}
		$this->assign('list',$results);
		$this->display();
	}

	/*======================================================================*\
		Function:	worklogPublish
		Purpose:	发布工作日志
		Input:		无	            空
		Output:		模板输出      	#
	\*======================================================================*/
	public function worklogPublish(){
		$Worklog = M('Worklog');
		$User = M('UserProfile');
		$param = $this->_param();
		$uid = $this->_uid;
		if($param['work']){
			$data = array(
				'owner_uid'	=> $uid,
				'title'=> $param['title'],
				'work' => $param['work'],
				'remark' => $param['remark'],
				'create_time' => datetime(),
				'update_time' => datetime(),
				'status' => 1
			);

			$pulisher = $User->field('name')->where("owner_uid = '$uid'")->find();
			$per_worklog = array(
				'pulisher' => $pulisher['name'],
				'create_time' => datetime(),
			);

			$v = $Worklog->add($data);
			if($v){
				$this->ajaxReturn($per_worklog,'添加成功',1);
			}else{
				$this->ajaxReturn(0,'添加失败',0);
			}
			
		}else{
			$this->display();//$this->ajaxReturn(0,'日志内容不能为空',0);
		}
	}
	/*======================================================================*\
		Function:	worklogEdit
		Purpose:	客户分类管理- 修改分类
		Input:		无	            空
		Output:		模板输出      	#
	\*======================================================================*/
	public function worklogEdit(){
		$Worklog = M('Worklog');
		$param = $this->_param();
		$uid = $this->_uid;
		if($param['worklog_id'] && $param['work']){
			$data = array(
				'work'	=> $param['work'],
				'update_time' => datetime()
			);
			$workLogID = intval($param['worklog_id']);
			$v = $Worklog->where("uid = '$uid' AND worklog_id = $workLogID")->save($data);
			if($v){
				logs($uid,"工作日志修改成功",1);
				$this->ajaxReturn(1,'工作日志修改成功',1);
			}else{
				logs($uid,"工作日志修改失败",0);
				$this->ajaxReturn(0,'工作日志修改失败',0);
				
			}
		}else{
			$this->ajaxReturn(0,'工作日志不能为空',0);
		}

	}
	/*======================================================================*\
		Function:	worklogDel
		Purpose:	客户分类管理- 删除分类
		Input:		无	            空
		Output:		模板输出      	#
	\*======================================================================*/
	public function worklogDel(){
		$Worklog = M('Worklog');
		$param = $this->_param();
		$uid = $this->_uid;
		if($param['worklog_id']){
			$data = array(
				'worklog_id'	=> $param['worklog_id'],
				'owner_uid' => $uid
			);
			$v = $Worklog->where($data)->delete();
			if($v){
				logs($uid,"工作日志删除成功",1);
				$this->ajaxReturn(1,"工作日志删除成功",1);
			}else{
				logs($uid,"工作日志删除失败",0);
				$this->ajaxReturn(0,"工作日志删除失败",0);
				
			}
		}else{
			$this->ajaxReturn(0,'获取工作日志ID失败',0);
		}
	}
	/*======================================================================*\
		Function:	getDepartment
		Purpose:	客户分类管理- 获取分类
		Input:		无	            空
		Output:		模板输出      	#
	\*======================================================================*/
	
	public function getWorklog(){
		$Worklog = M('Worklog');
		$param = $this->_param();
		$uid = $this->_uid;
		if($uid && $param['worklog_id']){
			$data = array(
				'owner_uid' => $uid,
				'worklog_id' => intval($param['worklog_id'])
			);
			$v = $Worklog->where($data)->find();
			if($v){
				$this->ajaxReturn($v,"分类获取成功",1);
			}else{				
				$this->ajaxReturn(0,"分类为空",0);
			}
		}else{
			$this->ajaxReturn(0,'你确定你登录了吗？',0);
		}

	}
	
	public function test(){
			$input = "10101010101010101010";
			$input = str_split($input);
			dump($input);
	}
	
}